<?php
/**
 * Created by PhpStorm.
 * User: cnpscy
 * Date: 2020/7/7
 * Time: 22:56
 * Note: Base.php
 */

namespace App\Base;

use App\Logger\Logger;
use App\Traits\LaravelTemplate;
use App\Traits\ThinkTemplate;
use EasySwoole\ORM\AbstractModel;

class AdminController extends BaseController
{
    use LaravelTemplate;
    //use ThinkTemplate;

    protected $model;
    protected $model_class;
    protected $params;

    private $view_name;
    private $template;

    public function beforeAction(?string $actionName, $request): void
    {
        $this->params = $request->getRequestParam();

        // 实例化模型
        if (!empty($this->model_class) && (empty($this->model) || !$this->model instanceof AbstractModel)) {
            $this->model = new $this->model_class;
        }

        /**
         * 根据路由解析视图前缀
         */
        // 根据beforeAction，获取请求方法所归属的类名，切忌不可更换 为 $actionName。beforeAction此方法在控制器调用指定方法之前操作
        if ($class = $this->getAllowMethodReflections()[__FUNCTION__]) {
            $controller_file = get_string_pluralize(strtolower(string_underscore_lowercase(\App\Library\Arr::last(explode('\\', $class->class)))));
            $controller_file = str_replace('_controllers', 's', $controller_file);
        } else {
            // 强制路由
            $controller_method = explode('/', ltrim(
                str_replace(
                    cnpscy_config('web_admin_prefix'),
                    '',
                    $this->server['path_info'] ?? $this->server['request_uri'])
                , '/'));
            $controller_file   = $controller_method[0];
        }
        $this->view_name = 'default/' . $controller_file . '/' . $actionName;

        $this->template = $this->load_template();


        $request_uri = (string)$request->getServerParams()['request_uri'];

        \App\Library\Config::set('_server.request_uri', $request_uri);

        $this->share([
            'method_name' => $actionName,
            'request_uri' => $request_uri,
        ]);
    }

    public function onRequest(?string $action): ?bool
    {
        return parent::onRequest($action);
    }


    function index()
    {
        return $this->fetch($this->view_name, [
            'list' => $this->model->paginate($this->params),
        ]);

        if ($this->model instanceof AbstractModel) {
            $list = $this->model->paginate();
            return $this->successJson($list);
        } else {
            return $this->successJson([], 1, '');
        }
    }

    function detail()
    {
        $data = $this->model->get($this->params['id']);
        return $this->fetch($this->view_name, [
            'detail' => $data,
        ]);


        if ($data) {
            return $this->successJson($data);
        } else {
            return $this->errorJson('信息不存在');
        }
    }

    function create()
    {
        if ($this->model->create($this->params)->save()) {
            return $this->successJson('添加成功');
        } else {
            return $this->errorJson('添加失败');
        }
    }

    function update()
    {
        $data = $this->model->get($this->params[$this->model->getPrimaryKey()]);
        if ($data->update($this->params)) {
            return $this->successJson('更新成功');
        } else {
            return $this->errorJson('更新失败');
        }
    }

    function delete()
    {
        $model = $this->model;
        $data  = $model->get($this->params[$model->getPrimaryKey()]);
        switch ($model->is_delete) {
            case 0:
                $result = $data->update([$model->delete_field => 1]);
                break;
            case 1:
                $result = $data->destroy();
                break;
        }
        if ($result) {
            return $this->successJson('删除成功');
        } else {
            return $this->errorJson('删除失败');
        }
    }

    function changeFiledStatus()
    {

    }

    function getSelectLists()
    {

    }
}